﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Data.SqlClient;
using Model.Entities;

namespace Model
{
    public class AlbumProvider : DataAccess
    {
        public AlbumProvider()
        {
        }

        protected AlbumDetails GetAlbumFromReader(IDataReader reader)
        {
            DateTime date = GetDate(reader["ReleaseDate"]);
            var album = new AlbumDetails((int) reader["AlbumID"], reader["AlbumName"].ToString(), 
                                        date, reader["Url"].ToString(), reader["Style"].ToString(), 
                                        (int)reader["ArtistID"], reader["ArtistName"].ToString(), 
                                        reader["Cover"].ToString());
            GetTracksForAlbum(album);
            return album;
        }

        public void GetTracksForAlbum(AlbumDetails album)
        {
            var trackProvider = new TrackProvider();
            var tracks = trackProvider.GetTracksByAlbumId(album.AlbumId);
            foreach (var tr in tracks)
            {
                album.Tracks.Add(tr);                
            }
        }

        public AlbumDetails GetEmptyAlbum()
        {
            var emptyAlbum = new AlbumDetails
                                 {
                                     AlbumId = 0,
                                     AlbumName = "Empty",
                                     Style = "Empty",
                                     ArtistId = 0,
                                     ArtistName = "Empty",
                                     Cover = "Empty"
                                 };
            return emptyAlbum;
        }

        public List<AlbumDetails> GetAlbums()
        {
            var con = new SqlConnection(ConnectionString);
            var cmd = new SqlCommand("GetAlbums", con) { CommandType = CommandType.StoredProcedure };
            var albums = new List<AlbumDetails>();
            
            con.Open();
            SqlDataReader reader = cmd.ExecuteReader();
            if (reader != null)
            {
                while (reader.Read())
                {
                    albums.Add(GetAlbumFromReader(reader));
                }
                reader.Close();
            }
            con.Close();
            return albums;
        }

        public int InsertAlbum(AlbumDetails alb)
        {
            var con = new SqlConnection(ConnectionString);
            var cmd = new SqlCommand("AddAlbum", con) { CommandType = CommandType.StoredProcedure };
            
            cmd.Parameters.Add("@AlbumName", SqlDbType.NVarChar).Value = alb.AlbumName;
            cmd.Parameters.Add("@ReleaseDate", SqlDbType.DateTime).Value = alb.ReleaseDate;
            cmd.Parameters.Add("@Url", SqlDbType.NVarChar).Value = alb.Url;
            cmd.Parameters.Add("@Style", SqlDbType.NVarChar).Value = alb.Style;
            cmd.Parameters.Add("@ArtistID", SqlDbType.NVarChar).Value = alb.ArtistId;
            if (string.IsNullOrEmpty(alb.Cover))
                alb.Cover = "noimage.gif";
            cmd.Parameters.Add("@Cover", SqlDbType.NVarChar).Value = alb.Cover;
            cmd.Parameters.Add("@AlbumID", SqlDbType.Int).Direction = ParameterDirection.Output;

            con.Open();
            cmd.ExecuteNonQuery();
            con.Close();
            return (int)cmd.Parameters["@AlbumID"].Value;
        }

        public void UpdateAlbum(AlbumDetails alb)
        {
            var con = new SqlConnection(ConnectionString);
            var cmd = new SqlCommand("UpdateAlbum", con) { CommandType = CommandType.StoredProcedure };

            cmd.Parameters.Add("@AlbumName", SqlDbType.NVarChar).Value = alb.AlbumName;
            cmd.Parameters.Add("@ReleaseDate", SqlDbType.DateTime).Value = alb.ReleaseDate;
            cmd.Parameters.Add("@Url", SqlDbType.NVarChar).Value = alb.Url;
            cmd.Parameters.Add("@Style", SqlDbType.NVarChar).Value = alb.Style;
            cmd.Parameters.Add("@ArtistID", SqlDbType.NVarChar).Value = alb.ArtistId;
            if (string.IsNullOrEmpty(alb.Cover))
                alb.Cover = "noimage.gif";
            cmd.Parameters.Add("@Cover", SqlDbType.NVarChar).Value = alb.Cover;
            cmd.Parameters.Add("@AlbumID", SqlDbType.Int).Value = alb.AlbumId;
           
            con.Open();
            cmd.ExecuteNonQuery();
            con.Close();
        }

        public void DeleteAlbum(AlbumDetails alb)
        {
            var con = new SqlConnection(ConnectionString);
            var cmd = new SqlCommand("DeleteAlbum", con) { CommandType = CommandType.StoredProcedure };

            cmd.Parameters.Add("@AlbumID", SqlDbType.Int).Value = alb.AlbumId;

            con.Open();
            cmd.ExecuteNonQuery();
            con.Close();
        }

        public List<AlbumDetails> GetAlbumsByName(string albName)
        {
            if (string.IsNullOrEmpty(albName))
                return GetAlbums();
            var con = new SqlConnection(ConnectionString);
            var cmd = new SqlCommand("SearchAlbumByName", con) { CommandType = CommandType.StoredProcedure };
            cmd.Parameters.Add("@AlbumName", SqlDbType.NVarChar).Value = albName;
            var albums = new List<AlbumDetails>();

            con.Open();
            SqlDataReader reader = cmd.ExecuteReader();
            if (reader != null)
            {
                while (reader.Read())
                {
                    albums.Add(GetAlbumFromReader(reader));
                }
                reader.Close();
            }
            con.Close();
            return albums;
        }

        public AlbumDetails GetAlbumById(int albId)
        {
            var con = new SqlConnection(ConnectionString);
            var cmd = new SqlCommand("SearchAlbumById", con) { CommandType = CommandType.StoredProcedure };
            cmd.Parameters.Add("@AlbumId", SqlDbType.Int).Value = albId;
            var album = new AlbumDetails();

            con.Open();
            SqlDataReader reader = cmd.ExecuteReader();
            if (reader != null)
            {
                while (reader.Read())
                {
                    album = GetAlbumFromReader(reader);
                }
                reader.Close();
            }
            con.Close();
            return album;
        }
    }
}
